package lib;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Database {
    protected static final String DriverUrl = "com.mysql.jdbc.Driver";
    protected String _userName;
    protected String _userPasswd;
    protected String _dns;
    
    protected Connection _connection;
    protected Statement _connStatement;
    
    public Database() {
        this._dns       = "jdbc:mysql://localhost:3306/poll_sys";
        this._userName  = "root";
        this._userPasswd= "root";
    }
    
    /**
     * 数据库的构造函数
     *
     * @param host 主机路径
     * @param port 端口
     * @param dbName 数据库名
     * @param userName 用户名
     * @param userPasswd 用户密码
     */
    public Database(String host,
                     String port,
                     String dbName,
                     String userName,
                     String userPasswd) {
        this._connection    = null;
        this._connStatement = null;
        this._userName      = userName;
        this._userPasswd    = userPasswd;
        this._dns           = this.getDns(host, port, dbName);
    }
   
    /**
     * 重载一下数据库的构造函数 
     */
    public Database(String dns, String userName, String userPasswd) {
        this._userName      = userName;
        this._userPasswd    = userPasswd;
        this._dns           = this.getDns(dns);
    }
    
    /**
     * 得到数据库的连接字符串
     * 
     * @param host 主机地址
     * @param port 端口号
     * @param dbName 数据库名
     */
    protected String getDns(String host, String port, String dbName) {
        return String.format("jdbc:mysql://%s:%s/%s", host, port, dbName);
    }
   
    /**
     * 重载数据库的连接字符串生成方法
     * 
     * @param dns 有了主机，端口号，数据库名信息
     */
    protected String getDns(String dns) {
        return String.format("jdbc:mysql://%s", dns);
    }
   
    /**
     * 连接数据
     * 
     * @return 是否连接成功
     */
    public boolean connect() {
    	try {
    	    Class.forName(DriverUrl);
    	    this._connection   = DriverManager.getConnection(this._dns,
                                                	         this._userName,
                                                	         this._userPasswd);
            this._connStatement= this.genStatement();
    	    System.out.println("^_^\tConnectin ok!");
    	    return true;
    	} catch(Exception ex) {
    	    System.out.println(ex.getMessage());
    	    return false;
    	}
    }
    
    /**
     * 得到查询的结果集 
     */
    public ResultSet query(String sql) {
        try {
            return this._connStatement.executeQuery(sql);
        } catch(Exception ex) {
            System.out.println("Get Statement failed!!");
            return null;
        }
    }
   
    /**
     * 插件操作
     * 
     * @param String sql 执行语句
     * @return boolean true or false 是否成功
     */
    public boolean insert(String sql) {
        return this.update(sql);
    }
  
    /**
     * 更新操作
     * 
     * @param String sql 执行语句
     * @return boolean true or false 是否成功
     */
    public boolean update(String sql) {
        try {
            PreparedStatement ps    = this._connection.prepareStatement(sql);
            if(ps.executeUpdate() > 0) {
                return true;
            }
            return false;
        } catch(Exception ex) {
            System.out.println(ex.getMessage());
            return false;
        }
    }
   
    /**
     * 删除操作
     * 
     * @param String sql 执行语句
     * @return boolean true or false 是否成功
     */
    public boolean delete(String sql) {
        return this.update(sql);
    }
    
    /**
     * 得到数据库连接状态对象 
     */
    protected Statement genStatement() {
        try {
            if(this._connection != null && this._connection.isClosed()) {
                return null;
            }
            return this._connection.createStatement();
        } catch(Exception ex) {
            System.out.println(ex.getMessage());
            return null;
        }
    }
  
    /**
     * 关闭数据库连接 
     */
    public boolean closeConnection() {
        try {
            if(!this._connStatement.isClosed()) {
                this._connStatement.close();
            }
            if(!this._connection.isClosed()) {
                this._connection.close();
            }
            return true;
        } catch(Exception ex) {
            return false;
        }
    }
    
}
